#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <regex>
#include <sys/stat.h>
#include "../server.hpp"


#define DEFALT_TIMEOUT 10


std::unordered_map<int, std::string> _statu_msg = {
    {100,  "Continue"},
    {101,  "Switching Protocol"},
    {102,  "Processing"},
    {103,  "Early Hints"},
    {200,  "OK"},
    {201,  "Created"},
    {202,  "Accepted"},
    {203,  "Non-Authoritative Information"},
    {204,  "No Content"},
    {205,  "Reset Content"},
    {206,  "Partial Content"},
    {207,  "Multi-Status"},
    {208,  "Already Reported"},
    {226,  "IM Used"},
    {300,  "Multiple Choice"},
    {301,  "Moved Permanently"},
    {302,  "Found"},
    {303,  "See Other"},
    {304,  "Not Modified"},
    {305,  "Use Proxy"},
    {306,  "unused"},
    {307,  "Temporary Redirect"},
    {308,  "Permanent Redirect"},
    {400,  "Bad Request"},
    {401,  "Unauthorized"},
    {402,  "Payment Required"},
    {403,  "Forbidden"},
    {404,  "Not Found"},
    {405,  "Method Not Allowed"},
    {406,  "Not Acceptable"},
    {407,  "Proxy Authentication Required"},
    {408,  "Request Timeout"},
    {409,  "Conflict"},
    {410,  "Gone"},
    {411,  "Length Required"},
    {412,  "Precondition Failed"},
    {413,  "Payload Too Large"},
    {414,  "URI Too Long"},
    {415,  "Unsupported Media Type"},
    {416,  "Range Not Satisfiable"},
    {417,  "Expectation Failed"},
    {418,  "I'm a teapot"},
    {421,  "Misdirected Request"},
    {422,  "Unprocessable Entity"},
    {423,  "Locked"},
    {424,  "Failed Dependency"},
    {425,  "Too Early"},
    {426,  "Upgrade Required"},
    {428,  "Precondition Required"},
    {429,  "Too Many Requests"},
    {431,  "Request Header Fields Too Large"},
    {451,  "Unavailable For Legal Reasons"},
    {501,  "Not Implemented"},
    {502,  "Bad Gateway"},
    {503,  "Service Unavailable"},
    {504,  "Gateway Timeout"},
    {505,  "HTTP Version Not Supported"},
    {506,  "Variant Also Negotiates"},
    {507,  "Insufficient Storage"},
    {508,  "Loop Detected"},
    {510,  "Not Extended"},
    {511,  "Network Authentication Required"}
};

std::unordered_map<std::string, std::string> _mime_msg = {
    {".aac",        "audio/aac"},
    {".abw",        "application/x-abiword"},
    {".arc",        "application/x-freearc"},
    {".avi",        "video/x-msvideo"},
    {".azw",        "application/vnd.amazon.ebook"},
    {".bin",        "application/octet-stream"},
    {".bmp",        "image/bmp"},
    {".bz",         "application/x-bzip"},
    {".bz2",        "application/x-bzip2"},
    {".csh",        "application/x-csh"},
    {".css",        "text/css"},
    {".csv",        "text/csv"},
    {".doc",        "application/msword"},
    {".docx",       "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
    {".eot",        "application/vnd.ms-fontobject"},
    {".epub",       "application/epub+zip"},
    {".gif",        "image/gif"},
    {".htm",        "text/html"},
    {".html",       "text/html"},
    {".ico",        "image/vnd.microsoft.icon"},
    {".ics",        "text/calendar"},
    {".jar",        "application/java-archive"},
    {".jpeg",       "image/jpeg"},
    {".jpg",        "image/jpeg"},
    {".js",         "text/javascript"},
    {".json",       "application/json"},
    {".jsonld",     "application/ld+json"},
    {".mid",        "audio/midi"},
    {".midi",       "audio/x-midi"},
    {".mjs",        "text/javascript"},
    {".mp3",        "audio/mpeg"},
    {".mpeg",       "video/mpeg"},
    {".mpkg",       "application/vnd.apple.installer+xml"},
    {".odp",        "application/vnd.oasis.opendocument.presentation"},
    {".ods",        "application/vnd.oasis.opendocument.spreadsheet"},
    {".odt",        "application/vnd.oasis.opendocument.text"},
    {".oga",        "audio/ogg"},
    {".ogv",        "video/ogg"},
    {".ogx",        "application/ogg"},
    {".otf",        "font/otf"},
    {".png",        "image/png"},
    {".pdf",        "application/pdf"},
    {".ppt",        "application/vnd.ms-powerpoint"},
    {".pptx",       "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
    {".rar",        "application/x-rar-compressed"},
    {".rtf",        "application/rtf"},
    {".sh",         "application/x-sh"},
    {".svg",        "image/svg+xml"},
    {".swf",        "application/x-shockwave-flash"},
    {".tar",        "application/x-tar"},
    {".tif",        "image/tiff"},
    {".tiff",       "image/tiff"},
    {".ttf",        "font/ttf"},
    {".txt",        "text/plain"},
    {".vsd",        "application/vnd.visio"},
    {".wav",        "audio/wav"},
    {".weba",       "audio/webm"},
    {".webm",       "video/webm"},
    {".webp",       "image/webp"},
    {".woff",       "font/woff"},
    {".woff2",      "font/woff2"},
    {".xhtml",      "application/xhtml+xml"},
    {".xls",        "application/vnd.ms-excel"},
    {".xlsx",       "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
    {".xml",        "application/xml"},
    {".xul",        "application/vnd.mozilla.xul+xml"},
    {".zip",        "application/zip"},
    {".3gp",        "video/3gpp"},
    {".3g2",        "video/3gpp2"},
    {".7z",         "application/x-7z-compressed"}
};


class Util{
    public:
        //字符串分割函数,将src字符串按照sep字符进行分割，得到的各个字串放到arry中，最终返回字串的数量
        static size_t Split(const std::string &src,const std::string &seq,std::vector<std::string>* array){
            //abc,def
            int idx=0;
            while(idx<src.size()){
                int pos=src.find(seq,idx);
                if(pos==std::string::npos){
                    array->push_back(src.substr(idx));
                    break;
                }
                if(pos-idx>0)
                    array->push_back(src.substr(idx,pos-idx));
                idx=pos+seq.size();
            }
            return array->size();
        }
        //读取文件的所有内容，将读取的内容放到一个Buf中
        static bool ReadFile(const std::string& filename,std::string* buf){
            std::ifstream ifs(filename,std::ios::binary);
            if(ifs.is_open()==false){
                printf("open %s file failed",filename.c_str());
                return false;
            }
            size_t fsize=0;
            ifs.seekg(0,ifs.end);//跳转读写位置到末尾
            fsize=ifs.tellg();//获取当前读写位置相对于起始位置的偏移量，从末尾偏移刚好就是文件大小
            ifs.seekg(0,ifs.beg);//跳转到起始位置
            buf->resize(fsize);
            ifs.read(&(*buf)[0],fsize);
            if(ifs.good()==false){
                printf("read %s file failed",filename.c_str());
                ifs.close();
                return false;
            }
            ifs.close();
            return true;
        }
        //向文件写入数据
        static bool WriteFile(const std::string& filename,const std::string& buf ){
            std::ofstream ofs(filename,std::ios::binary | std::ios::trunc);
            if(ofs.is_open()==false){
                printf("open %s file failed",filename.c_str());
                return false;
            }
            ofs.write(buf.c_str(),buf.size());
            if(ofs.good()==false){
                printf("write %s file failed",filename.c_str());
                ofs.close();
                return false;
            }
            ofs.close();
            return true;
        }
        //URL编码，避免URL中资源路径与查询字符串中的特殊字符与HTTP请求中特殊字符产⽣歧义
        //编码格式：将特殊字符的ascii值，转换为两个16进制字符，前缀% C++ -> C%2B%2B
        // 不编码的特殊字符： RFC3986⽂档规定 . - _ ~ 字⺟，数字属于绝对不编码字符
        //RFC3986⽂档规定，编码格式 %HH 转成16进制
        //W3C标准中规定，查询字符串中的空格，需要编码为+， 解码则是+转空格
        static std::string UrlEncode(const std::string url,bool convert_space_to_plus){
            std::string res;
            for(auto &c:url){
                if(c=='.' || c=='-' || c=='_' || c=='~'  || isalnum(c)){
                    res+=c;
                    continue;
                }
                if(c==' ' && convert_space_to_plus==true){
                    res+='+';
                    continue;
                }
                //剩下的字符都是需要编码成为 %HH 格式
                char tmp[4]={0};
                snprintf(tmp,4,"%%%02x",c);
                res+=tmp;
            }
            return res;
        }
        static char HEXTOI(char c){
            if(c >='0' && c<='9'){
                return c-'0';
            }else if(c>='a' && c<='z'){
                return c-'a'+10;
            }else if(c>='A' && c<='Z'){
                return c-'A'+10;
            }
            return -1;
        }
        static std::string  UrlDecode(const std::string url,bool convert_space_to_plus){
            //遇到了%，则将紧随其后的2个字符，转换为数字，第⼀个数字左移4位，然后加上第⼆个数字 + -> 2b %2b->2 << 4 + 11
            std::string res;
            for(int i=0;i<url.size();++i){
                if(url[i]=='+'&&convert_space_to_plus==true){
                    res+=' ';
                    continue;
                }
                if(url[i]=='%' && (i+2) <url.size()){
                    char v1=HEXTOI(url[i+1]);
                    char v2=HEXTOI(url[i+2]);
                    char v=v1*16+v2;
                    res+=v;
                    i+=2;
                    continue;
                }
                res+=url[i];
            }
            return res;
        }

        //响应状态码的描述信息获取
        static std::string StatuDesc(int statu){
            auto it=_statu_msg.find(statu);
            if(it!=_statu_msg.end()){
                return it->second;
            }
            return "Unknow";
        }
        //根据⽂件后缀名获取⽂件mime
        static std::string ExtMine(const std::string &filename){
            //a.txt先获取文件扩展名
            size_t pos=filename.find_last_of('.');
            if(pos==std::string::npos){
                return "application/octet-stream";
            }
            //根据扩展名，获取mine
            std::string ext=filename.substr(pos);
            auto it=_mime_msg.find(ext);
            if(it!=_mime_msg.end()){
                return it->second;
            }
            return "application/octet-stream";
        }
        //判断⼀个⽂件是否是⼀个⽬录
        static bool IsDirectory(const std::string &filename){
            struct stat st;
            int ret=stat(filename.c_str(),&st);
            if(ret<0){
                return false;
            }
            return S_ISDIR(st.st_mode);
        }
        //判断⼀个⽂件是否是⼀个普通⽂件
        static bool IsRegular(const std::string &filename){
            struct stat st;
            int ret=stat(filename.c_str(),&st);
            if(ret<0){
                return false;
            }
            return S_ISREG(st.st_mode);
        }
        //http请求的资源路径有效性判断
        // "/index.html" --- 前边的/叫做相对根⽬录 映射的是某个服务器上的⼦⽬录
        // 想表达的意思就是，客⼾端只能请求相对根⽬录中的资源，其他地⽅的资源都不予理会
        // "/../login", 这个路径中的..会让路径的查找跑到相对根⽬录之外，这是不合理的，不安全的
        static bool ValidPaht(const std::string &path){
            //思想：按照/进⾏路径分割，根据有多少⼦⽬录，计算⽬录深度，有多少层，深度不能⼩于0
            std::vector<std::string> subdir;
            Split(path,"/",&subdir);
            int level=0;
            for(auto &dir:subdir){
                if(dir==".."){
                    level--;
                    if(level<0) return false;
                    continue;
                }
                level++;
            }
            return true;
        }
};


class HttpRequest {
    public:
        std::string _method;    //请求方法
        std::string _path;      //资源路径
        std::string _version;    //协议版本
        std::string _body;      //请求正文
        std::smatch _matches;   //资源路径的正则表达式
        std::unordered_map<std::string,std::string> _headers;   //头部字段
        std::unordered_map<std::string,std::string> _params;    //查询字符串
    public:
        HttpRequest():_version("HTTP/1.1"){}
        void ReSet(){
            _method.clear();
            _path.clear();
            _version="HTTP/1.1";
            _body.clear();
            std::smatch sm;
            _matches.swap(sm);
            _headers.clear();
            _params.clear();
        }
        //插入头部字段
        void SetHeader(const std::string &key,const std::string& val){
            _headers.insert(std::make_pair(key,val));
        }
        //判断是否存在指定头部字段
        bool HasHeader(const std::string& key) const{
            auto it=_headers.find(key);
            if(it!=_headers.end()){
                return true;
            }
            return false;
        }
        //获取指定头部字段的值
        std::string GetHeader(const std::string &key) const {
            auto it=_headers.find(key);
            if(it!=_headers.end()){
                return it->second;
            }
            return "";
        }
        //插入查询字符串
        void SetParam(const std::string& key,const std::string &val){
            _params.insert(std::make_pair(key,val));
        }
        //判断是否有某个指定的查询字符串
        bool HashParam(const std::string& key) const{
            auto it=_params.find(key);
            if(it!=_params.end()){
                return true;
            }
            return false;
        }
        //获取指定查询字符串
        std::string GetParam(const std::string& key) const {
            auto it=_params.find(key);
            if(it!=_params.end()){
                return it->second;
            }
            return "";
        }
        size_t ContentLength() const {
            // Content-Length: 1234\r\n
            bool ret=HasHeader("Content-Length");
            if(ret==false){
                return 0;
            }
            std::string lenStr=GetHeader("Content-Length");
            return std::stol(lenStr);
        }
        //判断是否有短链接
        bool Close()const {
            // 没有Connection字段，或者有Connection但是值是close，则都是短链接，否则就是长连接
            if(HasHeader("Connection")==true && GetHeader("Connection")=="keep-alive"){
                return false;
            }
            return true;
        }
};

class HttpResponse{
    public:
        int _statu;
        bool _redirect_flag;//是否重定向标志位
        std::string _body;
        std::string _redirect_url;//重定向地址
        std::unordered_map<std::string,std::string> _headers;
    public:
        HttpResponse():_redirect_flag(false),_statu(200){}
        HttpResponse(int statu):_redirect_flag(false),_statu(statu){}
        void ReSet(){
            _statu=200;
            _headers.clear();
            _body.clear();
            _redirect_url.clear();
            _redirect_flag=false;
        }
        void SetHeader(const std::string &key,const std::string &val){
            _headers.insert(std::make_pair(key,val));
        }
        std::string GetHeader(const std::string &key) const{
            auto it=_headers.find(key);
            if(it!=_headers.end()){
                return it->second;
            }
            return "";
        }
        //判断是否存在指定头部字段
        bool HasHeader(const std::string& key) const{
            auto it=_headers.find(key);
            if(it!=_headers.end()){
                return true;
            }
            return false;
        }
        void SetContent(const std::string &body,const std::string &type="text/html"){
            _body=body;
            SetHeader("Content-Type",type);
        }
        //302 临时重定向  301永久重定向
        void SetRedirect(const std::string &url,int statu =302){
            _redirect_url=url;
            _redirect_flag=true;
            _statu=statu;
        }
        //判断是否有短链接
        bool Close()const {
            // 没有Connection字段，或者有Connection但是值是close，则都是短链接，否则就是长连接
            if(HasHeader("Connection")==true && GetHeader("Connection")=="keep-alive"){
                return false;
            }
            return true;
        }
};

#define MAX_LINE 8192

typedef enum{
    RECV_HTTP_ERROR,
    RECV_HTTP_LINE,
    RECV_HTTP_HEAD,
    RECV_HTTP_BODY,
    RECV_HTTP_OVER
}HttpRecvStatu;
class HttpContext {
    private:
        int _resp_statu;    //响应状态码
        HttpRecvStatu _recv_statu;  //当前接收及解析的阶段状态
        HttpRequest _request;   //已经解析到的请求信息
    private:
        bool ParseHttpLine(const std::string &line){
            std::smatch matches;
            //HTTP请求行格式： GET /hdm/login?user=zhangsan&pass=123123 HTTP/1.1\r\n
            std::regex e("(GET|HEAD|POST|PUT|DELETE) ([^?]*)(?:\\?(.*))? (HTTP/1\\.[01])(?:\n|\r\n)?",std::regex::icase);//std::regex::icase忽略大小写
            //"(GET|HEAD|POST|PUT|DELETE) ([^?]*)\\?(.*) (HTTP/1\\.[01])(?:\n|\r\n)?"
            //GET|HEAD|POST|PUT|DELETE 表示匹配并提取其中任意一个字符
            //[^?]* [^?]匹配非问号字符，后面的*表示0次或多次
            //\\?   \\?表示原始的?  字符(.*)表示提取?之后的任意多个字符0次或多次，直到遇到空格
            //(?:\n|\r\n)?  (?:...)     表示匹配某个格式的字符串，但是不提取，最后的?表示的是匹配前边的表达式0次或多次
            bool ret =std::regex_match(line,matches,e);
            if(ret==false){
                _recv_statu=RECV_HTTP_ERROR;
                _resp_statu=400;//BAD REQUEST
                return false;
            }
            // 0:GET /hdm/login?user=zhangsan&pass=123123 HTTP/1.1
            // 1:GET
            // 2:/hdm/login
            // 3:user=zhangsan&pass=123123
            // 4:HTTP/1.1
            //请求⽅法的获取
            _request._method=matches[1];
            std::transform(_request._method.begin(),_request._method.end(),_request._method.begin(),::toupper);//小写字母转换成大写
            //资源路径的获取，需要进⾏URL解码操作，但是不需要+转空格
            _request._path=Util::UrlDecode(matches[2],false);
            //协议版本的获取
            _request._version=matches[4];
            //查询字符串的获取与处理
            std::vector<std::string> array;
            std::string query_string=matches[3];
            //查询字符串的格式 key=val&key=val....., 先以 & 符号进⾏分割，得到各个字串
            Util::Split(query_string,"&",&array);
            //针对各个字串，以 = 符号进⾏分割，得到key 和val， 得到之后也需要进⾏URL解码
            for(auto &str:array){
                size_t pos=str.find("=");
                if(pos==std::string::npos){
                    _recv_statu=RECV_HTTP_ERROR;
                    _resp_statu=400;
                    return false;
                }
                std::string key=Util::UrlDecode(str.substr(0,pos),true);
                std::string val=Util::UrlDecode(str.substr(pos+1),true);
                _request.SetParam(key,val);
            }
            return true;
        }
        bool RecvHttpLine(Buffer *buf){ 
            if(_recv_statu!=RECV_HTTP_LINE) return false;
            //1. 获取⼀⾏数据，带有末尾的换⾏
            std::string line=buf->GetLineAndPop();
            //2. 需要考虑的⼀些要素：缓冲区中的数据不⾜⼀⾏， 获取的⼀⾏数据超⼤
            if(line.size()==0){
                //缓冲区中的数据不⾜⼀⾏，则需要判断缓冲区的可读数据⻓度，如果很⻓了都不⾜⼀⾏，这是有问题的
                if(buf->ReadAbleSize()>MAX_LINE){
                    _recv_statu = RECV_HTTP_ERROR;
                    _resp_statu = 414;//URI TOO LONG
                    return false;
                }
                //缓冲区中数据不⾜⼀⾏，但是也不多，就等等新数据的到来
                return true;
            }
            if(line.size()>MAX_LINE){
                _recv_statu = RECV_HTTP_ERROR;
                _resp_statu = 414;//URI TOO LONG
                return false;
            }
            bool ret=ParseHttpLine(line);
            if(ret==false){
                return false;
            }
            //⾸⾏处理完毕，进⼊头部获取阶段
            _recv_statu=RECV_HTTP_HEAD;
            return true;
        }
        bool RecvHttpHead(Buffer* buf){
            if(_recv_statu!=RECV_HTTP_HEAD) return false;
            //⼀⾏⼀⾏取出数据，直到遇到空⾏为⽌， 头部的格式 key: val\r\nkey:val\r\n....
            while(1){
                std::string line=buf->GetLineAndPop();
                if(line.size()==0){
                //缓冲区中的数据不⾜⼀⾏，则需要判断缓冲区的可读数据⻓度，如果很⻓了都不⾜⼀⾏，这是有问题的
                if(buf->ReadAbleSize()>MAX_LINE){
                    _recv_statu = RECV_HTTP_ERROR;
                    _resp_statu = 414;//URI TOO LONG
                    return false;
                }
                //缓冲区中数据不⾜⼀⾏，但是也不多，就等等新数据的到来
                return true;
                }
                if(line.size()>MAX_LINE){
                    _recv_statu = RECV_HTTP_ERROR;
                    _resp_statu = 414;//URI TOO LONG
                    return false;
                }
                if(line=="\n" || line=="\r\n"){
                    break;
                }
                bool ret=ParseHttpHead(line);
                if(ret==false) return false;
            }
            //头部处理完毕，进⼊正⽂获取阶段
            _recv_statu=RECV_HTTP_BODY;
            return true;
        }
        bool ParseHttpHead(std::string& line){
            //key: val\r\nkey: val\r\n....
            if(line.back()=='\n') line.pop_back();//末尾是换⾏则去掉换⾏字符
            if(line.back()=='\r') line.pop_back();
            size_t pos=line.find(": ");
            if(pos==std::string::npos){
                _recv_statu = RECV_HTTP_ERROR;
                _resp_statu = 400;
                return false;
            }
            std::string key=line.substr(0,pos);
            std::string val=line.substr(pos+2);
            _request.SetHeader(key,val);
            return true;

        }
        bool RecvHttpBody(Buffer* buf){
            if(_recv_statu!=RECV_HTTP_BODY) return false;
            //1. 获取正文长度
            size_t content_length=_request.ContentLength();
            if(content_length==0){
                //没有正⽂，则请求接收解析完毕
                _recv_statu = RECV_HTTP_OVER;
                return true;
            }
            //2. 当前已经接收了多少正⽂,其实就是往 _request._body 中放了多少数据了
            size_t real_len=content_length-_request._body.size();
            //3. 接收正⽂放到body中，但是也要考虑当前缓冲区中的数据，是否是全部的正⽂
            // 3.1 缓冲区中数据，包含了当前请求的所有正⽂，则取出所需的数据
            if(buf->ReadAbleSize()>=real_len){
                _request._body.append(buf->ReadPosition(),real_len);
                buf->MoveReadOffset(real_len);
                _recv_statu=RECV_HTTP_OVER;
                return true;
            }
            // 3.2 缓冲区中数据，⽆法满⾜当前正⽂的需要，数据不⾜，取出数据，然后等待新数据到来
            _request._body.append(buf->ReadPosition(),buf->ReadAbleSize());
            buf->MoveReadOffset(buf->ReadAbleSize());
            return true;
        }
    public:
        HttpContext():_resp_statu(200),_recv_statu(RECV_HTTP_LINE){}
        void Reset(){
            _resp_statu=200;
            _recv_statu=RECV_HTTP_LINE;
            _request.ReSet();
        }
        int RespStatu(){ return _resp_statu; }
        HttpRecvStatu RecvStatu(){ return _recv_statu; }
        HttpRequest &Request(){ return _request; }
        //接收并解析HTTP请求
        void RecvHttpRequest(Buffer* buf){
            //不同的状态，做不同的事情，但是这⾥不要break， 因为处理完请求⾏后，应该⽴即处理头部，⽽不是退出等新数据
            switch(_recv_statu){
                case RECV_HTTP_LINE: RecvHttpLine(buf);
                case RECV_HTTP_HEAD: RecvHttpHead(buf);
                case RECV_HTTP_BODY: RecvHttpBody(buf);
            }
            return;
        }
};


class HttpServer{
    private:
        using Handler=std::function<void(const HttpRequest&,HttpResponse*)>;
        using Handlers=std::vector<std::pair<std::regex,Handler>>;
        Handlers _get_route;
        Handlers _post_route;
        Handlers _put_route;
        Handlers _delete_route;

        std::string _basedir;//静态资源根目录
        TcpServer _server;
    private:
        void ErrorHandler(HttpResponse* rsp){
            //组织一个错误展示页面
            std::string body;
            body+="<html>";
            body+="<head>";
            body+="<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>";
            body+="</head>";
            body+="<body>";
            body+="<h1>";
            body+=std::to_string(rsp->_statu);
            body+=" ";
            body+=Util::StatuDesc(rsp->_statu);
            body+="</h1>";
            body+="</body>";
            body+="</html>";
            //将数据当作响应正文放到rsp中
            rsp->SetContent(body,"text/html");
        }
        //将HttpResponse中的要素按http格式进行组织发送
        void WriteReponse(const PtrConnection& conn,const HttpRequest& req,HttpResponse& rsp){
            //1.完善头部字段
            if(req.Close()==true){
                rsp.SetHeader("Connection","close");
            }else{
                rsp.SetHeader("Connection","keep-alive");
            }
            if(rsp._body.empty()==false && rsp.HasHeader("Content-Length")==false){
                rsp.SetHeader("Content-Length",std::to_string(rsp._body.size()));
            }
            if(rsp._body.empty()==false && rsp.HasHeader("Content-Type")==false){
                rsp.SetHeader("Content-Type","application/octet-stream");
            }
            if(rsp._redirect_flag==true){
                rsp.SetHeader("Location",rsp._redirect_url);
            }
            //2. 将rsp中的要素，按照http格式组织
            std::stringstream rsp_ss;
            rsp_ss<<req._version<<" "<<std::to_string(rsp._statu)<<" "<< Util::StatuDesc(rsp._statu)<<"\r\n";
            for(auto& head: rsp._headers){
                rsp_ss<<head.first<<": "<<head.second<<"\r\n";
            }
            rsp_ss<<"\r\n";
            rsp_ss<<rsp._body;
            //发送数据
            conn->Send(rsp_ss.str().c_str(),rsp_ss.str().size());
        }
        bool IsFileHandler(const HttpRequest&req){
            //1. 必须设置了静态资源根目录
            if(_basedir.empty()) return false;
            //2. 请求方法必须是GET / HEAD 请求方法
            if(req._method!="GET" && req._method!="HEAD") return false;
            //3. 请求的资源路径必须是一个合法路径
            if(Util::ValidPaht(req._path)==false) return false;
            //4. 请求的资源必须存在
            // 有⼀种请求⽐较特殊 -- ⽬录：/, /image/， 这种情况给后边默认追加⼀个index.html
            // index.html /image/a.png
            // 前缀的相对根⽬录,也就是将请求路径转换为实际存在的路径/image/a.png -> ./wwwroot/image/a.png
            std::string req_path=_basedir+ req._path;
            if(req._path.back()=='/'){
                req_path+="index.html";
            }
            if(Util::IsRegular(req_path)==false) return false;
            return true;
        }
        //静态资源请求处理
        void FileHandler(HttpRequest& req,HttpResponse* rsp){
            std::string req_path=_basedir+req._path;
            if(req._path.back()=='/'){
                req_path+="index.html";
            }
            bool ret=Util::ReadFile(req_path,&rsp->_body);
            if(ret==false) return;
            std::string mine=Util::ExtMine(req_path);
            rsp->SetHeader("Content-Type",mine);
        }
        //功能性请求的分类处理
        void Dispatcher(HttpRequest& req,HttpResponse* rsp,Handlers& handlers){
            //在对应的请求方法路由表中，查找是否含有对应资源请求的处理函数，有就调用没有就404
            //使用正则表达式对请求的资源路径进行正则表达匹配
            for(auto& handler :handlers){
                std::regex &re=handler.first;
                Handler& functor=handler.second;
                bool ret=std::regex_match(req._path,req._matches,re);
                if(ret==false) continue;
                return functor(req,rsp);//执行处理函数 
            }
            rsp->_statu=404;
        }
        void Route(HttpRequest& req,HttpResponse* rsp){
            //1.对请求进行分辨究竟是一个静态资源请求还是功能性请求
            //如果并非是静态资源请求，也并非是功能性请求则返回405
            if(IsFileHandler(req)==true){
                return FileHandler(req,rsp);
            }
            if(req._method=="GET" || req._method=="HEAD"){
                return Dispatcher(req,rsp,_get_route);
            }else if(req._method=="POST"){
                return Dispatcher(req,rsp,_post_route);
            }else if(req._method=="PUT"){
                return Dispatcher(req,rsp,_put_route);
            }else if(req._method=="DELETE"){
                return Dispatcher(req,rsp,_delete_route);
            }
            rsp->_statu=405;//Method Not Allowed
        }
        //设置上下文
        void OnConnected(const PtrConnection& conn){
            conn->SetContext(HttpContext());
            DBG_LOG("NEW CONNECTION:%p",conn.get());
        }
        //缓冲区数据解析+处理
        void OnMessage(const PtrConnection& conn,Buffer* buffer){
            while(buffer->ReadAbleSize()>0){
                //1.获取上下文
                HttpContext* context=conn->GetContext()->get<HttpContext>();
                //2. 通过上下文对缓冲区进行解析获取HttpRequst对象
                //  2.1 如果解析出错了，就直接回复错误对象
                //  2.2 如果正常，而且请求已经获取完毕，才开始出处理
                context->RecvHttpRequest(buffer);
                HttpResponse rsp(context->RespStatu());
                HttpRequest &req=context->Request();
                if(context->RespStatu()>=400){
                    //进行错误响应，关闭连接
                    ErrorHandler(&rsp);//填充一个错误显示页面数据到rsp中
                    WriteReponse(conn,req,rsp);
                    context->Reset();
                    buffer->MoveReadOffset(buffer->ReadAbleSize());
                    conn->Shutdown();
                    return;
                }
                if(context->RecvStatu()!=RECV_HTTP_OVER){
                    //当前请求还没有接收完毕，等数据接收完毕再处理
                    return;
                }
                //3.请求路由+业务处理
                Route(req,&rsp);
                //4.对HttResponse组织发送
                WriteReponse(conn,req,rsp);
                //5. 重置上下文
                context->Reset();
                //6. 根据长短连接判断是否关闭连接或者继续处理
                if(rsp.Close()==true) conn->Shutdown();
            }
        }
    public:
        HttpServer(int port,int timeout=DEFALT_TIMEOUT):_server(port){
            _server.EnableInactiveRelease(timeout);
            _server.SetConnectedCallback(std::bind(&HttpServer::OnConnected,this,std::placeholders::_1));
            _server.SetMessageCallback(std::bind(&HttpServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
        }
        void SetBaseDir(const std::string &path) {
            assert(Util::IsDirectory(path)==true);
            _basedir=path; 
        }
        void Get(const std::string &pattern,const Handler& handler){
            _get_route.push_back(std::make_pair(std::regex(pattern),handler));
        }
        void Post(const std::string &pattern,const Handler& handler){
            _post_route.push_back(std::make_pair(std::regex(pattern),handler));
        }
        void Put(const std::string &pattern,const Handler& handler){
            _put_route.push_back(std::make_pair(std::regex(pattern),handler));
        }
        void Delete(const std::string &pattern,const Handler& handler){
            _delete_route.push_back(std::make_pair(std::regex(pattern),handler));
        }
        void SetThreadCount(int count){ 
            return _server.SetThreadCount(count); 
        }
        void Listen(){
            _server.Start();
        }

};

 